package edu.umn.contactviewer.utils;

import android.content.Context;
import android.util.Log;

import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class FileOps {
    public static String readFile(Context ctxt) {
        try {
            FileInputStream fis = ctxt.getApplicationContext().openFileInput(Constants.PERSISTENCE_FILE);
            StringBuffer content = new StringBuffer("");
            int ch;
            while((ch=fis.read()) != -1) {
                content.append((char)ch);
            }
            fis.close();
            return content.toString();
        }
        catch(Exception e){
            Log.i("ContactViewer", "Exception e = " + e);
            return null;
        }
    }
    
    public static void writeFile(Context ctxt, String output) {
        try{
            OutputStream os = ctxt.getApplicationContext().openFileOutput(Constants.PERSISTENCE_FILE, Context.MODE_PRIVATE);
            OutputStreamWriter osw = new OutputStreamWriter(os);
            osw.write(output);
            osw.close();
        }
        catch(Exception e) {
            Log.i("ContactViewer", "Exception e = " + e);
        }
    }
}
